package com.ityu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ityu.exception.IllegalDataException;
import com.ityu.exception.InsertSameException;
import com.ityu.mapper.StudentMapper;
import com.ityu.pojo.Student;
import com.ityu.pojo.pagePojo.StudentPage;
import com.ityu.pojo.result.PageResult;
import com.ityu.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> pageFindStudents(StudentPage studentPage) {
        log.info("-----StudentService-----分页查询学生！参数为{}",studentPage);
        PageHelper.startPage(studentPage.getPage(),studentPage.getPageSize());
        Page<Student> result = (Page<Student>) studentMapper.pageFindStudents(studentPage);
        return new PageResult<Student>(result.getTotal(),result.getResult());
    }

    @Override
    public void deleteStudents(List<Integer> ids) {
        log.info("-----StudentService-----批量删除学生信息，参数：{}", ids);
        studentMapper.bachDeleteStudents(ids);
    }

    @Override
    public void addStudent(Student student) {
        log.info("-----StudentService-----新增学生信息，参数：{}", student);
        try {
            student.setCreateTime(LocalDateTime.now());
            student.setUpdateTime(LocalDateTime.now());
            studentMapper.addStudent(student);
        } catch ( DuplicateKeyException e ) {
            log.error("学生"+ student.getName() +"已存在");
            throw new InsertSameException("学生信息已存在");
        } catch (Exception e){
            log.error("添加学生失败,未知错误");
            System.out.println(e);
            throw new RuntimeException("添加学生失败");
        }
    }

    @Override
    public Student selectStudentById(Integer id) {
        log.info("-----StudentService-----查询学生信息，参数：{}", id);
        return studentMapper.selectStudentById(id);
    }

    @Override
    public void updateStudent(Student student) {
        log.info("-----StudentService-----更新学生信息，参数：{}", student);
        student.setUpdateTime(LocalDateTime.now());
        try {
            studentMapper.updateStudent(student);
        }catch (DuplicateKeyException e){
            log.warn("学生修改信息重复！");
            throw new InsertSameException("修改信息不合法或冲突，请重试！");
        }
        catch (Exception e) {
            log.error("学生数据修改失败，未知错误！");
            throw new RuntimeException(e);
        }
    }

    @Override
    public void addViolation(Integer id, Integer score) {
        log.info("-----StudentService-----添加学生违规，参数：{}", id);
        if (score <= 0){
            throw new IllegalDataException("违规分数不能为负数!");
        }
        Student student = studentMapper.selectStudentById(id);
        student.setViolationCount((short)(student.getViolationCount()+1));
        student.setViolationScore((short)(student.getViolationScore()+score));
        if (student.getViolationScore() > 255){
            throw new IllegalDataException("违规分数超过最大值!");
        }
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent( student);
    }
}
